package com.example.controller;

import com.example.pojo.*;
import com.example.service.ClassOrderService;
import com.example.service.ClassTableService;
import com.example.service.EquipmentService;
import com.example.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ClassTableService classTableService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ClassOrderService classOrderService;

    @Autowired
    private EquipmentService equipmentService;
    //跳转个人信息页面
    @RequestMapping("/toUserInfo")
    public String toUserInformation(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "userInformation";
    }

    //跳转修改个人信息页面
    @RequestMapping("/toUpdateInfo")
    public String toUpdateUserInformation(HttpSession session, Model model) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "updateUserInformation";
    }

    //修改个人信息
    @RequestMapping("/updateInfo")
    public String updateUserInformation(HttpSession session, Member member) {
        Member member1 = (Member) session.getAttribute("user");

        member.setMemberAccount(member1.getMemberAccount());
        member.setCardClass(member1.getCardClass());
        member.setCardTime(member1.getCardTime());
        member.setCardNextClass(member1.getCardNextClass());

        memberService.updateMemberByMemberAccount(member);
        return "userInformation";
    }

    //跳转我的课程页面
    @RequestMapping("/toUserClass")
    public String toUserClass(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        Integer memberAccount = member.getMemberAccount();
        List<ClassOrder> classOrderList = classOrderService.selectClassOrderByMemberAccount(memberAccount);
        model.addAttribute("classOrderList", classOrderList);
        return "userClass";
    }

    //退课
    @RequestMapping("delUserClass")
    public String deleteUserClass(Integer classOrderId) {
        classOrderService.deleteByClassOrderId(classOrderId);
        return "redirect:toUserClass";
    }

    //跳转报名选课页面
    @RequestMapping("/toApplyClass")
    public String toUserApplyClass(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        List<ClassTable> classList = classTableService.findAll();
        model.addAttribute("member", member);
        model.addAttribute("classList", classList);
        return "userApplyClass";
    }

    //报名选课
    @RequestMapping("/applyClass")
    public String userApplyClass(Integer classId, Model model, HttpSession session) {
        ClassTable classTable = classTableService.selectByClassId(classId);
        Member member = (Member) session.getAttribute("user");

        Integer classId1 = classTable.getClassId();
        String className = classTable.getClassName();
        String coach = classTable.getCoach();
        String classBegin = classTable.getClassBegin();
        String memberName = member.getMemberName();
        Integer memberAccount = member.getMemberAccount();

        ClassOrder classOrder = new ClassOrder(classId1, className, coach, memberName, memberAccount, classBegin);
        Integer memberAccount1 = member.getMemberAccount();
        ClassOrder classOrder1 = classOrderService.selectMemberByClassIdAndMemberAccount(classId1, memberAccount1);

        if (classOrder1 == null) {
            classOrderService.insertClassOrder(classOrder);
        }

        return "redirect:toUserClass";
    }

    /**
     * 预定器材
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/toOrderEquipment")
    public String toOrderEquipment(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        List<Equipment> equipments = equipmentService.findRelaxEquipment();
        for(int i=0;i<equipments.size();i++){
            String relaxStatus = equipments.get(i).getEquipmentRelax();
            if(relaxStatus.equals("1")){
                relaxStatus = "空闲";
                equipments.get(i).setEquipmentRelax(relaxStatus);
            }else{
                relaxStatus = "使用中";
                equipments.get(i).setEquipmentRelax(relaxStatus);
            }
        }
        model.addAttribute("member", member);
        model.addAttribute("equipmentList", equipments);
        return "userSelectEquipment";
    }

    /**
     * 更新relax状态并且生成订单
     * @param equipmentId
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/take")
    @Transactional
    public String orderEquipment(Integer equipmentId,Model model,HttpSession session){
        Member member = (Member) session.getAttribute("user");
        Integer userId = member.getMemberAccount();
        Equipment equipment = equipmentService.getById(equipmentId);
        equipmentService.updateStatus(equipmentId);
        EquipmentOrder equipmentOrder = new EquipmentOrder();
        equipmentOrder.setEquipmentId(equipment.getEquipmentId());
        equipmentOrder.setEquipmentName(equipment.getEquipmentName());
        equipmentOrder.setEquipmentLocation(equipment.getEquipmentLocation());
        equipmentOrder.setUserId(userId);
        Boolean success = equipmentService.insertEquipmentOrder(equipmentOrder);
        Integer id = equipmentOrder.getEquipmentOrderId();
        session.setAttribute("orderId",id);
        if(!success)return null;
        model.addAttribute("orderId",id);
        model.addAttribute("useTime",System.currentTimeMillis());
        return "userTime";
    }

    /**
     * 设置预定时间
     * @param useTime
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/setTime")
    public String updateEquipmentTime(String useTime, Model model, HttpSession session){
        Integer orderId =(Integer) session.getAttribute("orderId");
        Instant instant = Instant.ofEpochMilli(Long.parseLong(useTime));
        Timestamp timestamp = Timestamp.from(instant);
        equipmentService.updateTime(timestamp, orderId);
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "userInformation";
    }

    /**
     * 查看我的预定信息
     * @param useTime
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/getMy")
    public String getOrdersByUserId(String useTime, Model model, HttpSession session){
        Member member = (Member) session.getAttribute("user");
        Integer userId = member.getMemberAccount();
        List<EquipmentOrder> orders = equipmentService.getByUserId(userId);
        model.addAttribute("orders",orders);
        return "userOrderEquipment";
    }


    /**
     * 删除预订单
     * @param orderId
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/delete")
    public String deleteOrder(Integer orderId, Model model, HttpSession session){
        EquipmentOrder order = equipmentService.getOrder(orderId);
        equipmentService.deleteOrderedEquipment(orderId);
        equipmentService.updateStatusAfterExit(order.getEquipmentId());
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "userInformation";
    }
}
